package packagename.states
{
	// FLASH
	import flash.events.*;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
    import flash.ui.ContextMenuBuiltInItems;
    import flash.events.ContextMenuEvent;
    import flash.events.ContextMenuEvent;
	import flash.text.TextField;
	import flash.net.*;
		
	// HIVE
	import org.hive.managers.plugin.LayoutManager;
	import org.hive.managers.plugin.LoadManager;
	import org.hive.managers.plugin.PapervisionManager;
	import org.hive.managers.plugin.SoundManager;
	import org.hive.managers.StateManager;
	import org.hive.state.*
	import org.hive.sequence.*
		
	// PROJECT NAME
	import packagename.data.ProjectNameContentList;
	import packagename.states.ProjectNamePreloadingState;
	
	
	public class ProjectNameMainState extends State implements IState
	{
		private var myContextMenu:ContextMenu = new ContextMenu();
		
		public override function create():ActionSequence
		{
			//________________________________________________________________________ INIT MANAGERS
			LayoutManager.init();
			LoadManager.init();
			
			// I like to force the resize event 
			LayoutManager.stage.dispatchEvent( new Event(Event.RESIZE) );
			
			contentList = contentList as ProjectNameContentList;
			//________________________________________________________________________ SEQUENCE
			var createSequence:ActionSequence = new ActionSequence( "ProjectNameMainState / createSequence" );
							
			myContextMenu.hideBuiltInItems();
			addCustomMenuItems();
			LayoutManager.documentClass.contextMenu = myContextMenu;
            		
			createSequence.push( new SequenceFunction(StateManager, StateManager.switchState, new ProjectNamePreloadingState()) );
			
			return createSequence;
		}

        private function removeDefaultItems():void 
		{
			myContextMenu.hideBuiltInItems();
            var defaultItems:ContextMenuBuiltInItems = LayoutManager.documentClass.contextMenu.builtInItems;
            defaultItems.print = true;
        }

        private function addCustomMenuItems():void 
		{
            var item:ContextMenuItem = new ContextMenuItem("ProjectName blog");
			myContextMenu.customItems.push(item);
            item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelectHandler);
        }

        private function menuSelectHandler(event:ContextMenuEvent):void 
		{
            trace("menuSelectHandler: " + event);
        }

        private function menuItemSelectHandler(event:ContextMenuEvent):void 
		{
            trace("menuItemSelectHandler: " + event);
			sendToURL( event );
        }
		
		 private function sendToURL( e:Event ):void
		{
			navigateToURL( new URLRequest( contentList.getContentByName( 'blog' ) ), "_blank" );
		}
    				
		public override function close():CloseSequence
		{
			var closeSequence:CloseSequence = new CloseSequence( "Main::closeSequence" );
			return closeSequence;
		}
		
	}
	
}
